﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormChiTietMatHang : Form
    {
        private NIEN_LUAN.Db.DataContentDataContext db = new NIEN_LUAN.Db.DataContentDataContext();
        private FormQuanLyMatHang formquanlymathang;
        private FormTimChiTietMatHang formTimChiTietMatHang;
        private FormTimChiTietHangXuat formTimChiTietHangXuat;
        private FormLapPhieuNhap frmlapphieunhap;
        private FormTimKiemMatHang frmTimKiemMatHang;
        public Db.MATHANG MatHangMuonSua;
        public Db.LOAIHANG LoaiHangAdd;

        public FormChiTietMatHang(FormQuanLyMatHang frm)
        {
            formquanlymathang = frm;
            InitializeComponent();
        }

        public FormChiTietMatHang(FormTimChiTietMatHang frm)
        {
            formTimChiTietMatHang = frm;
            InitializeComponent();
        }

        public FormChiTietMatHang(FormTimChiTietHangXuat frm)
        {
            formTimChiTietHangXuat = frm;
            InitializeComponent();
        }

        public FormChiTietMatHang(FormLapPhieuNhap frm)
        {
            frmlapphieunhap = frm;
            InitializeComponent();
        }


        public FormChiTietMatHang(Db.MATHANG model, FormQuanLyMatHang frm)
        {
            InitializeComponent();
            this.txtMaMH.Text = model.MaHang;
            this.txtMaMH.Enabled = false;
            this.txtTenMH.Text = model.TenHang;
            this.cmbDVtinh.Text = model.MaDonViTinh;
            this.comboBoxMaLH.Text = model.MaLoaiHang;
            this.numericUDgiaban.Value = model.GiaBan.Value;
            this.numericUDGianhap.Value = model.GiaNhap.Value;
            this.numericUDSLHC.Value = model.SoLuongHienCo.Value;
            MatHangMuonSua = model;
            formquanlymathang = frm;
        }

        public FormChiTietMatHang(Db.MATHANG model, FormTimKiemMatHang frm)
        {   
            InitializeComponent();
            this.txtMaMH.Text = model.MaHang;
            this.txtMaMH.Enabled = false;
            this.txtTenMH.Text = model.TenHang;
            this.cmbDVtinh.Text = model.MaDonViTinh;
            this.comboBoxMaLH.Text = model.MaLoaiHang;
            this.numericUDgiaban.Value = model.GiaBan.Value;
            this.numericUDGianhap.Value = model.GiaNhap.Value;
            this.numericUDSLHC.Value = model.SoLuongHienCo.Value;
            MatHangMuonSua = model;
            frmTimKiemMatHang = frm;
        }

        private void btnThoatCTMH_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;//khi click vao day thi tra ve dialog result la cancel
            this.Close();
        }
    
        private void btnThemCTMH_Click(object sender, EventArgs e)
        {
            //kiểm tra hợp lệ dữ liệu
            if (string.IsNullOrEmpty(this.txtMaMH.Text.Trim()))
            {
                MessageBox.Show("Mã hàng không được rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if(string.IsNullOrEmpty(this.txtTenMH.Text.Trim()))
            {
                MessageBox.Show("Tên hàng rỗng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtTenMH.Focus();
                return;
            }

            if (this.numericUDgiaban.Value <= 0)
            {
                MessageBox.Show("Giá bán phải lớn hon 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.numericUDgiaban.Focus();
                return;
            }

            //if (this.numericUDSLHC.Value<=0)
            //{
            //    MessageBox.Show("Số lượng hiện có phải lớn hơn 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    this.numericUDSLHC.Focus();
            //    return;
            //}

            var donvitinh=bsdonvitinh.Current as Db.DONVITINH;
            if(donvitinh==null)
            {
                MessageBox.Show("Chưa chọn đơn vị tính", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var loaihang = bsLoaiHang.Current as Db.LOAIHANG;
            if (loaihang == null)
            {
                MessageBox.Show("Chưa chọn loại hàng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }           

            var mathang = new Db.MATHANG 
            {
                MaDonViTinh=donvitinh.MaDonViTinh,
                MaLoaiHang=loaihang.MaLoaiHang,
                SoLuongHienCo=this.numericUDSLHC.Value,
                TenHang=this.txtTenMH.Text,
                MaHang=this.txtMaMH.Text,
                GiaBan=this.numericUDgiaban.Value, 
                GiaNhap=this.numericUDGianhap.Value
            };

            if (formquanlymathang != null&&MatHangMuonSua==null)
            {
                var mh = (from m in db.MATHANGs
                          where m.MaHang == this.txtMaMH.Text.Trim()
                          select m).ToList();
                if (mh.Count > 0)
                {
                    MessageBox.Show("Mã trùng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                db.MATHANGs.InsertOnSubmit(mathang);
                db.SubmitChanges();
                this.formquanlymathang.MatHangadd = mathang;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }

            if (formTimChiTietMatHang != null)
            {
                var mh = (from m in db.MATHANGs
                          where m.MaHang == this.txtMaMH.Text.Trim()
                          select m).ToList();
                if (mh.Count > 0)
                {
                    MessageBox.Show("Mã trùng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                db.MATHANGs.InsertOnSubmit(mathang);
                db.SubmitChanges();
                this.formTimChiTietMatHang.MatHangadd = mathang;
                this.DialogResult = DialogResult.OK;
            }

            if (formTimChiTietHangXuat != null)
            {
                var mh = (from m in db.MATHANGs
                          where m.MaHang == this.txtMaMH.Text.Trim()
                          select m).ToList();
                if (mh.Count > 0)
                {
                    MessageBox.Show("Mã trùng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                db.MATHANGs.InsertOnSubmit(mathang);
                db.SubmitChanges();
                this.formTimChiTietHangXuat.MatHangadd = mathang;               
                this.DialogResult = DialogResult.OK;
            }

            if (MatHangMuonSua != null && formquanlymathang != null)//sua chi tiet khach hang 
            {
                ///
                var _mathang = db.MATHANGs.Single(m => m.MaHang == mathang.MaHang);
                _mathang.TenHang = mathang.TenHang;
                _mathang.MaDonViTinh = mathang.MaDonViTinh;
                _mathang.MaLoaiHang = mathang.MaLoaiHang;
                _mathang.SoLuongHienCo = mathang.SoLuongHienCo;
                _mathang.GiaBan = mathang.GiaBan;
                _mathang.GiaNhap=mathang.GiaNhap;
                db.SubmitChanges();
                this.formquanlymathang.MatHangSua = _mathang;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }

            if (MatHangMuonSua != null && frmTimKiemMatHang != null)//sua chi tiet khach hang 
            {
                ///
                var _mathang = db.MATHANGs.Single(m => m.MaHang == mathang.MaHang);
                _mathang.TenHang = mathang.TenHang;
                _mathang.MaDonViTinh = mathang.MaDonViTinh;
                _mathang.MaLoaiHang = mathang.MaLoaiHang;
                _mathang.SoLuongHienCo = mathang.SoLuongHienCo;
                _mathang.GiaBan = mathang.GiaBan;
                _mathang.GiaNhap = mathang.GiaNhap;
                db.SubmitChanges();
                this.frmTimKiemMatHang.MatHangSua = _mathang;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void FormChiTietMatHang_Load(object sender, EventArgs e)
        {
            //Lấy tất cả đơn vị tính về
            var danhsachdonvitinh = (from mh in db.DONVITINHs
                                     select mh).ToList();

            //Gán Bsdonvitinh = danh sách các đơn vị tính
            bsdonvitinh.DataSource = danhsachdonvitinh;

            //gán nguồn của combobox đơn vị tính
            this.cmbDVtinh.DataSource = bsdonvitinh;

            //chỉ hiển thị tên đơn vị tính
            this.cmbDVtinh.DisplayMember = "TenDonViTinh";

            //Lấy tất cả các loại mặt hàng
            var danhsachmathang = (from mh in db.LOAIHANGs
                                   select mh).ToList();

            bsLoaiHang.DataSource = danhsachmathang;

            this.comboBoxMaLH.DataSource = bsLoaiHang;

            this.comboBoxMaLH.DisplayMember = "TenLoaiHang";

        }
        private void btnChonLH_Click(object sender, EventArgs e)
        {
            var kq = new FormChiTietLoaiHang(this).ShowDialog();
            if (kq == DialogResult.OK)
            {
                var loaihang = (from m in db.LOAIHANGs
                           select m).ToList();
                bsLoaiHang.DataSource = loaihang;
                MessageBox.Show("Đã thêm một loại hàng thành công");
                this.bsLoaiHang.MoveLast();
            }
        }
    }
}
